﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LiveWebServiceBase;
using LiveWebServiceBase.ServiceUserSettings;
using LiveWebService.PSW;

namespace LiveWebServiceAdmin.ActionPages
{
    public partial class managedomain : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            string manageuser = string.Empty;
            string manangeorg = string.Empty;
            string manangesite = string.Empty;
            string editaccountID = string.Empty;

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                manageuser = Page.Request.QueryString["manageuser"];
            else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
                manageuser = this.User.Identity.Name;

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageorganization"]))
                manangeorg = Page.Request.QueryString["manageorganization"];

            if (!string.IsNullOrEmpty(Page.Request.QueryString["managesite"]))
                manangesite = Page.Request.QueryString["managesite"];

            if (!string.IsNullOrEmpty(Page.Request.QueryString["editeac"]))
                editaccountID = Page.Request.QueryString["editeac"];

            if (!string.IsNullOrEmpty(manageuser) && !string.IsNullOrEmpty(manangeorg) &&
                !string.IsNullOrEmpty(manangesite) && !string.IsNullOrEmpty(editaccountID))
            {
                cbIsNewPSAdmin.Visible = false;
                cbIsNewEXAdmin.Visible = false;

                UserSettings usItem = new UserSettings(manageuser);

                Account account = usItem.GetAccount(manangesite, editaccountID);

                tbNewDomainAdmin.Text = account.domainAdm;
                tbNewDomainAdminPwd.Text = account.domainAdmPwd;
                RequiredFieldValidator8.Enabled = false;

                tbNewEwsAdmin.Text = account.ewsAdm;
                tbNewEwsAdminPwd.Text = account.ewsAdmPwd;
                RequiredFieldValidator12.Enabled = false;

                tbNewPSAdmin.Text = account.psAdm;
                tbNewPSAdminPwd.Text = account.psAdmPwd;
                RequiredFieldValidator10.Enabled = false;

                btCreateNewAccount.Text = "Save";
            }
        }

        protected void btCreateNewAccount_Click(object sender, EventArgs e)
        {
            try
            {
                lblErrorTextNewAccount.Text = "";
                string manageuser = string.Empty;
                string manangeorg = string.Empty;
                string manangesite = string.Empty;
                string editaccountID = string.Empty;

                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                    manageuser = Page.Request.QueryString["manageuser"];
                else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
                    manageuser = this.User.Identity.Name;

                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageorganization"]))
                    manangeorg = Page.Request.QueryString["manageorganization"];

                if (!string.IsNullOrEmpty(Page.Request.QueryString["managesite"]))
                    manangesite = Page.Request.QueryString["managesite"];

                if (!string.IsNullOrEmpty(Page.Request.QueryString["editeac"]))
                    editaccountID = Page.Request.QueryString["editeac"];

                UserSettings usItem = new UserSettings(manageuser);

                Account newaccount = new Account()
                    {
                        domainAdm = tbNewDomainAdmin.Text,
                        domainAdmPwd = tbNewDomainAdminPwd.Text,
                        ewsAdm = tbNewEwsAdmin.Text,
                        ewsAdmPwd = tbNewEwsAdminPwd.Text,
                        psAdm = tbNewPSAdmin.Text,
                        psAdmPwd = tbNewPSAdminPwd.Text
                    };

                #region Create and impersonate Users
                LivePSW livepsw = null;

                if (cbIsNewPSAdmin.Checked || cbIsNewEXAdmin.Checked)
                    livepsw = new LivePSW("systemSession", tbNewDomainAdmin.Text, tbNewDomainAdminPwd.Text);

                try
                {
                    if (cbIsNewPSAdmin.Checked && livepsw != null)
                    {
                        livepsw.CreateNewMailbox(tbNewPSAdmin.Text, tbNewPSAdmin.Text, 0, tbNewPSAdminPwd.Text, "PSW Administrator", "", "");
                        try
                        {
                            livepsw.ImpersonateUser(tbNewPSAdmin.Text, "PSWAdministrator");
                        }
                        catch { }
                    }

                    if (cbIsNewEXAdmin.Checked && livepsw != null)
                    {
                        livepsw.CreateNewMailbox(tbNewEwsAdmin.Text, tbNewEwsAdmin.Text, 0, tbNewEwsAdminPwd.Text, "EWS Administrator", "", "");
                        try
                        {
                            livepsw.ImpersonateUser(tbNewEwsAdmin.Text, "EWSAdministrtor");
                        }
                        catch { }
                    }

                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (livepsw != null)
                        livepsw.CloseSession();
                }
                #endregion

                if (string.IsNullOrEmpty(editaccountID))
                    usItem.CreateAccount(manangeorg, manangesite, newaccount);
                else
                    usItem.EditAccount(manangeorg, manangesite, editaccountID, newaccount);

                IsCompleted.Value = "true";
            }
            catch (Exception ex)
            {
                lblErrorTextNewAccount.Text = ex.Message;
            }
        }
    }
}
